//
//  LineChartViewController2.m
//  cocoapodText
//
//  Created by apple on 16/8/6.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "ShiPei.h"
//导入时需要手动打上，系统没有提示
#import "Charts-Swift.h"
#import <Foundation/Foundation.h>
#import "LineChartViewController2.h"
@interface LineChartViewController2 ()
@end

@implementation LineChartViewController2

LineChartView *sampleLineChartView2;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self initLineChartView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)initLineChartView{
    sampleLineChartView2=[[LineChartView alloc] initWithFrame:CGRectMake(kScreenW/4, kScreenH/4, kScreenW/2, kScreenH/2)];
    sampleLineChartView2.backgroundColor = [UIColor whiteColor];
    sampleLineChartView2.delegate = self;
    [self.view addSubview:sampleLineChartView2];
    [self initLineChartStyleAndData];
}

-(void)initLineChartStyleAndData{
    [self setupBarLineChartView:sampleLineChartView2];
    [self setLineChartParam:sampleLineChartView2];
    [self setLegendStyle:sampleLineChartView2];
    [self initChartData];
}

- (void)setupBarLineChartView:(BarLineChartViewBase *)chartView
{
    //描述
    chartView.descriptionText = @"descriptionText";
    //无数据默认显示
    chartView.noDataTextDescription = @"You need to provide data for the chart.";
    //网格背景
    chartView.drawGridBackgroundEnabled = NO;
    chartView.dragEnabled = YES;
    [chartView setScaleEnabled:YES];
    chartView.pinchZoomEnabled = YES;
    chartView.backgroundColor = [UIColor colorWithWhite:204/255.f alpha:1.f];
}

-(void)setLineChartParam:(LineChartView *)chartView{
    ChartXAxis *xAxis = chartView.xAxis;
    xAxis.labelFont = [UIFont systemFontOfSize:12.f];
    xAxis.labelTextColor = UIColor.whiteColor;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.drawAxisLineEnabled = NO;
    xAxis.spaceBetweenLabels = 1.0;
    
    ChartYAxis *leftAxis = chartView.leftAxis;
    leftAxis.labelTextColor = [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f];
    leftAxis.axisMaxValue = 200.0;
    leftAxis.axisMinValue = 0.0;
    leftAxis.drawGridLinesEnabled = YES;
    leftAxis.drawZeroLineEnabled = NO;
    leftAxis.granularityEnabled = YES;
    
    ChartYAxis *rightAxis = chartView.rightAxis;
    rightAxis.labelTextColor = UIColor.redColor;
    rightAxis.axisMaxValue = 900.0;
    rightAxis.axisMinValue = -200.0;
    rightAxis.drawGridLinesEnabled = NO;
    rightAxis.granularityEnabled = NO;
}

//设置图例样式
-(void)setLegendStyle:(LineChartView *)chartView{
    
    chartView.legend.form = ChartLegendFormLine;
    //图例在柱状图中的位置
    chartView.legend.position = ChartLegendPositionBelowChartLeft;
    //图示样式: 方形、线条、圆形
    chartView.legend.form = ChartLegendFormLine;
    //图示大小
    chartView.legend.formSize = 9.0;
    //字体大小
    chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
    //间隔
    chartView.legend.xEntrySpace = 4.0;
    
    chartView.legend.textColor = UIColor.whiteColor;
}

- (void)initChartData
{
    [self setDataCount:19 range:30 chart:sampleLineChartView2];
}

- (void)setDataCount:(int)count range:(double)range chart:(LineChartView *)chartView
{
    //x轴文字
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++){
        [xVals addObject:[@(i) stringValue]];
    }
    
    //y轴高度
    NSMutableArray *yVals1 = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++){
        double mult = range / 2.0;
        double val = (double) (arc4random_uniform(mult)) + 50;
        [yVals1 addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    NSMutableArray *yVals2 = [[NSMutableArray alloc] init];
    for (int i = 0; i < count; i++){
        double mult = range;
        double val = (double) (arc4random_uniform(mult)) + 450;
        [yVals2 addObject:[[ChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    LineChartDataSet *set1 = nil, *set2 = nil;
    if (chartView.data.dataSetCount > 0){
        set1 = (LineChartDataSet *)chartView.data.dataSets[0];
        set2 = (LineChartDataSet *)chartView.data.dataSets[1];
        set1.yVals = yVals1;
        set2.yVals = yVals2;
        chartView.data.xValsObjc = xVals;
        [chartView.data notifyDataChanged];
        [chartView notifyDataSetChanged];
    }else {
        set1 = [[LineChartDataSet alloc] initWithYVals:yVals1 label:@"DataSet 1"];
        set1.axisDependency = AxisDependencyLeft;
        [set1 setColor:[UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f]];
        [set1 setCircleColor:UIColor.whiteColor];
        set1.lineWidth = 2.0;
        set1.circleRadius = 3.0;
        set1.fillAlpha = 65/255.0;
        set1.fillColor = [UIColor colorWithRed:51/255.f green:181/255.f blue:229/255.f alpha:1.f];
        set1.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
        set1.drawCircleHoleEnabled = NO;
        
        set2 = [[LineChartDataSet alloc] initWithYVals:yVals2 label:@"DataSet 2"];
        set2.axisDependency = AxisDependencyRight;
        [set2 setColor:UIColor.redColor];
        [set2 setCircleColor:UIColor.whiteColor];
        set2.lineWidth = 2.0;
        set2.circleRadius = 3.0;
        set2.fillAlpha = 65/255.0;
        set2.fillColor = UIColor.redColor;
        set2.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
        set2.drawCircleHoleEnabled = NO;
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set2];
        [dataSets addObject:set1];
        
        LineChartData *data = [[LineChartData alloc] initWithXVals:xVals dataSets:dataSets];
        [data setValueTextColor:UIColor.whiteColor];
        [data setValueFont:[UIFont systemFontOfSize:9.f]];
        
        chartView.data = data;
    }
}


#pragma mark - ChartViewDelegate

- (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry dataSetIndex:(NSInteger)dataSetIndex highlight:(ChartHighlight * __nonnull)highlight
{
    NSLog(@"chartValueSelected");
}

- (void)chartValueNothingSelected:(ChartViewBase * __nonnull)chartView
{
    NSLog(@"chartValueNothingSelected");
}

@end